package com.core.role.bo;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.core.role.dao.RoleDao;
import com.core.role.dto.RoleDto;
import com.core.role.dto.RoleSearchCondition;
import com.core.role.pojo.Role;
import com.core.user.dto.UserPo;
import com.core.user.dto.UserSearchCondition;
import com.core.user.pojo.SysUserRole;
import com.core.user.pojo.User;
import com.util.exception.ComRuntimeException;

@Service
public class RoleBo {
    @Autowired
    private RoleDao roleDao;

    public Role getById(Role role) {
		return roleDao.getById(role);
    }

    public void add(Role role) {
		validateAddUpdate(role, true);
		roleDao.add(role);
    }
    
    @Transactional
    public void batAdd(List<Role> lst,UserPo userPo) {
		//validateAddUpdate(userPo, true);
    	SysUserRole sysUserRole=null;
		for (Role role : lst) {
			//role.setId(userPo.getId());
			sysUserRole=new SysUserRole();
			sysUserRole.setUser_id(userPo.getId());
			sysUserRole.setRole_id(role.getId());
			roleDao.batAdd(sysUserRole);
		}
    }

    public void update(Role role) {
		validateAddUpdate(role, false);
		roleDao.update(role);
    }

    private void validateAddUpdate(Role role, boolean isAdd) {
		if (role == null) {
		    throw new ComRuntimeException("对象不能为空");
        }
    }

    public void delete(Role role) {
		roleDao.delete(role);
    }
    
    public void batDelete(List<Role> lst,UserPo userPo) {
    	SysUserRole sysUserRole=null;
		for (Role role : lst) {
			//role.setId(userPo.getId());
			sysUserRole=new SysUserRole();
			sysUserRole.setUser_id(userPo.getId());
			sysUserRole.setRole_id(role.getId());
			roleDao.batDelete(sysUserRole);
		}
    }

    public List<Role> searchPojo(RoleSearchCondition sc) {
		return roleDao.searchPojo(sc);
    }

    public List<RoleDto> searchDto(RoleSearchCondition sc) {
		return roleDao.searchDto(sc);
    }

    public Integer searchCnt(RoleSearchCondition sc) {
		return roleDao.searchCnt(sc);
    }

    //======================================= new method create here ====================================/

    public List<Role> searchAllRoles() {
        RoleSearchCondition sc = new RoleSearchCondition();
        return searchPojo(sc);
    }
    
    public List<Role> searchPojoByUserId(User user){
        return roleDao.searchPojoByUserId(user);
    }

    public List<Role> searchPojoByUserId1(UserSearchCondition usc){
        return roleDao.searchPojoByUserId1(usc);
    }
    
    public Integer searchPojoByUserIdCnt(UserSearchCondition usc){
        return roleDao.searchPojoByUserIdCnt(usc);
    }
    
    public List<Role> searchPojoByUserIdNot(UserSearchCondition usc){
        return roleDao.searchPojoByUserIdNot(usc);
    }
    
    public Integer searchPojoByUserIdNotCnt(UserSearchCondition usc){
        return roleDao.searchPojoByUserIdNotCnt(usc);
    }
    
    public Role getByCode(String code) {
        return roleDao.getByCode(code);
    }
}
